/*
 * Copyright (C) 2017-2021
 * All rights reserved, Designed By
 * Copyright authorization contact 18814114118
 */
package com.shop.cereshop.commons.result;


import com.shop.cereshop.commons.constant.CoReturnFormat;
import com.shop.cereshop.commons.exception.CoBusinessException;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

@Data
@ApiModel(value = "Result", description = "通用请求返回数据")
public class Result<T> {

    public Result() {
        this.code = "";
        this.message = "";
    }

    public Result(T data) {
        this.code = "";
        this.data = data;
    }


    public Result(T data, String name, String describe, String json) {
        this.code = "";
        this.data = data;
        this.name = name;
        this.describe = describe;
        this.json = json;
    }

    public Result(String name, String describe, String json) {
        this.code =  CoReturnFormat.SUCCESS;
        this.message = "";
        this.name = name;
        this.describe = describe;
        this.json = json;
    }

    public Result(T data, String code) {
        this.code = code;
        this.message = CoReturnFormat.getMessage(code);
        this.data = data;
    }

    public Result(T data, String code, String msg) {
        this.code = code;
        this.message = msg;
        this.data = data;
    }

    public Result(T data, String code, String name, String describe, String json) {
        this.code = code;
        this.message = CoReturnFormat.getMessage(code);
        this.data = data;
        this.name = name;
        this.describe = describe;
        this.json = json;
    }

    public Result(String code, CoBusinessException e) {
        this.code = code;
        this.message = e.getMessage();
        this.errorData = e.getErrorData();
    }

    public Result(String code, Object data) {
        this.code = code;
        this.message = CoReturnFormat.getMessage(code);
        this.errorData = data;
    }

    public Result(String code) {
        this.code = code;
        this.message = CoReturnFormat.getMessage(code);
    }

    public Result(String code, String name, String describe, String json) {
        this.code = code;
        this.message = CoReturnFormat.getMessage(code);
        this.name = name;
        this.describe = describe;
        this.json = json;
    }

    public Result(String code, String message, String name, String describe, String json) {
        this.code = code;
        this.message = message;
        this.name = name;
        this.describe = describe;
        this.json = json;
    }

    /**
     * 成功返回结果
     *
     * @param data 获取的数据
     * @param <T>  数据类型
     * @return Result
     */
    public static <T> Result<T> success(T data) {
        return new Result<>(data, CoReturnFormat.SUCCESS);
    }

    /**
     * 成功返回结果
     *
     * @param msg  返回信息
     * @param data 获取的数据
     * @param <T>  数据类型
     * @return Result
     */
    public static <T> Result<T> success(String msg, T data) {
        return new Result<>(data, CoReturnFormat.SUCCESS, msg);
    }

    /**
     * 成功返回结果
     *
     * @param <T> 数据类型
     * @return Result
     */
    public static <T> Result<T> success() {
        return new Result<>(CoReturnFormat.SUCCESS);
    }

    /**
     * 失败返回结果
     *
     * @param code 错误码
     * @param <T>  数据类型
     * @return Result
     */
    public static <T> Result<T> failed(String code) {
        return new Result<>(code);
    }

    /**
     * 失败返回结果
     *
     * @param code 错误码
     * @param <T>  数据类型
     * @return Result
     */
    public static <T> Result<T> failed(String code, T data) {
        return new Result<>(code, data);
    }

    /**
     * 失败返回结果
     *
     * @param code 错误码
     * @param msg  返回信息
     * @return Result
     */
    public static <T> Result<T> failed(String code, String msg) {
        return new Result<>(code, msg, "", "", "");
    }

    /**
     * 失败返回结果
     *
     * @param data 错误数据
     * @param <T>  数据类型
     * @return Result
     */
    public static <T> Result<T> failed(T data) {
        return new Result<>(CoReturnFormat.FAIL, data);
    }

    /**
     * 失败返回结果
     *
     * @param exception 业务异常
     * @param <T>       数据类型
     * @return Result
     */
    public static <T> Result<T> failed(CoBusinessException exception) {
        return new Result<>(CoReturnFormat.FAIL, exception);
    }


    /**
     * 返回结果
     *
     * @param code 错误码
     * @param msg  返回信息
     */
    public static <T> Result<T> custom(String code, String msg) {
        return new Result<>(code, msg, "", "", "");
    }

    /**
     * 参数验证失败返回结果
     *
     * @param <T> 数据类型
     * @return Result
     */
    public static <T> Result<T> validateFailed() {
        return failed(CoReturnFormat.PARAM_INVALID);
    }

    /**
     * 返回@validated参数校验异常信息
     *
     * @param exceptionMsg 异常信息
     */
    public static <T> Result<T> validatedException(String exceptionMsg) {
        return new Result<>(CoReturnFormat.PARAM_INVALID, exceptionMsg, "", "", "");
    }

    /**
     * 未登录返回结果
     *
     * @param <T> 数据类型
     * @return Result
     */
    public static <T> Result<T> unauthorized() {
        return failed(CoReturnFormat.USER_NOT_LOGIN);
    }

    /**
     * 未授权返回结果
     *
     * @param <T> 数据类型
     * @return Result
     */
    public static <T> Result<T> forbidden() {
        return failed(CoReturnFormat.TOKEN_APPROVE_ERROR);
    }

    /**
     * 返回编码code
     */
    @ApiModelProperty(value = "返回编码code")
    private String code;

    /**
     * 返回数据data
     */
    @ApiModelProperty(value = "返回数据data")
    private T data;

    /**
     * 错误数据
     */
    @ApiModelProperty(value = "错误数据")
    private Object errorData;

    /**
     * 返回信息
     */
    @ApiModelProperty(value = "返回信息")
    private String message;

    /**
     * 请求用户名
     */
    private String name;

    /**
     * 请求描述
     */
    private String describe;

    /**
     * 请求参数json
     */
    private String json;

}
